﻿using B.MESUI.Login.BasicData_UI.BasicData;
using DevExpress.ClipboardSource.SpreadsheetML;
using DevExpress.XtraEditors;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TextBox;

namespace B.MESUI.Login.BasicData_UI
{
    public partial class EditProjectLineForm : Form
    {
        private string _lineId;
        public event EventHandler EditSuccess;
        private DevExpress.XtraEditors.TextEdit textEdit1;
        private DevExpress.XtraEditors.TextEdit textEdit2;
        private DevExpress.XtraEditors.ComboBoxEdit comboBoxEdit2;
        private DevExpress.XtraEditors.TextEdit textEdit4;
        private DevExpress.XtraEditors.SimpleButton btnSave;
        private LabelControl labelControl1;
        private LabelControl labelControl2;
        private LabelControl labelControl3;
        private LabelControl labelControl4;
        private LabelControl labelControl5;
        private LabelControl labelControl6;
        private Dictionary<string, string> workshopNameIdMap = new Dictionary<string, string>();
        private DevExpress.XtraEditors.TextEdit textEdit3;
        private List<string> allWorkshops = new List<string>();
        public EditProjectLineForm(GetProjectLineDTO line)
        {
            InitializeComponent();
            _lineId = line.ID;
            textEdit3.Text = line.ID;
            textEdit3.Properties.NullText = "产线ID";
            textEdit1.Text = line.LINE_CODE;
            textEdit2.Text = line.LINE_NAME;
            comboBoxEdit2.Text = line.WORKSHOP_NAME; // 或 line.WORKSHOP_ID
            textEdit4.Text = line.LINE_DESCRIPTION;
            textEdit3.Properties.ReadOnly = true;
            btnSave.Click += btnSave_Click;
            LoadWorkShopList();
        }
        /// <summary>
        /// 获取车间下拉
        /// </summary>
        /// <returns></returns>
        private async Task LoadWorkShopList()
        {
            string url = "http://39.103.49.30:9982/api/WorkShop/GetAll";
            using (var client = new HttpClient())
            {
                var response = await client.GetAsync(url);
                if (response.IsSuccessStatusCode)
                {
                    var json = await response.Content.ReadAsStringAsync();
                    dynamic result = Newtonsoft.Json.JsonConvert.DeserializeObject(json);
                    var list = result.resultData;
                    comboBoxEdit2.Properties.Items.Clear();
                    workshopNameIdMap.Clear();
                    foreach (var item in list)
                    {
                        string id = item.id == null ? "" : item.id.ToString();
                        string name = item.workshoP_NAME == null ? "" : item.workshoP_NAME.ToString();
                        comboBoxEdit2.Properties.Items.Add(name);
                        if (!workshopNameIdMap.ContainsKey(name))
                            workshopNameIdMap.Add(name, id);
                    }
                }
            }
        }
        /// <summary>
        /// 一些按钮和控件的初始化设置。
        /// </summary>
        private void InitializeComponent()
        {
            this.textEdit1 = new DevExpress.XtraEditors.TextEdit();
            this.textEdit2 = new DevExpress.XtraEditors.TextEdit();
            this.comboBoxEdit2 = new DevExpress.XtraEditors.ComboBoxEdit();
            this.textEdit4 = new DevExpress.XtraEditors.TextEdit();
            this.btnSave = new DevExpress.XtraEditors.SimpleButton();
            this.labelControl1 = new DevExpress.XtraEditors.LabelControl();
            this.labelControl2 = new DevExpress.XtraEditors.LabelControl();
            this.labelControl3 = new DevExpress.XtraEditors.LabelControl();
            this.labelControl4 = new DevExpress.XtraEditors.LabelControl();
            this.labelControl5 = new DevExpress.XtraEditors.LabelControl();
            this.labelControl6 = new DevExpress.XtraEditors.LabelControl();
            this.textEdit3 = new DevExpress.XtraEditors.TextEdit();
            ((System.ComponentModel.ISupportInitialize)(this.textEdit1.Properties)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.textEdit2.Properties)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.comboBoxEdit2.Properties)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.textEdit4.Properties)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.textEdit3.Properties)).BeginInit();
            this.SuspendLayout();
            // 
            // textEdit1
            // 
            this.textEdit1.Enabled = false;
            this.textEdit1.Location = new System.Drawing.Point(210, 214);
            this.textEdit1.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);
            this.textEdit1.Name = "textEdit1";
            this.textEdit1.Properties.NullText = "产线编码";
            this.textEdit1.Size = new System.Drawing.Size(450, 44);
            this.textEdit1.TabIndex = 0;
            // 
            // textEdit2
            // 
            this.textEdit2.Location = new System.Drawing.Point(210, 294);
            this.textEdit2.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);
            this.textEdit2.Name = "textEdit2";
            this.textEdit2.Properties.NullText = "产线名称";
            this.textEdit2.Size = new System.Drawing.Size(450, 44);
            this.textEdit2.TabIndex = 1;
            // 
            // comboBoxEdit2
            // 
            this.comboBoxEdit2.Location = new System.Drawing.Point(210, 366);
            this.comboBoxEdit2.Name = "comboBoxEdit2";
            this.comboBoxEdit2.Properties.Buttons.AddRange(new DevExpress.XtraEditors.Controls.EditorButton[] {
            new DevExpress.XtraEditors.Controls.EditorButton(DevExpress.XtraEditors.Controls.ButtonPredefines.Combo)});
            this.comboBoxEdit2.Size = new System.Drawing.Size(450, 44);
            this.comboBoxEdit2.TabIndex = 7;
            // 
            // textEdit4
            // 
            this.textEdit4.Location = new System.Drawing.Point(210, 454);
            this.textEdit4.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);
            this.textEdit4.Name = "textEdit4";
            this.textEdit4.Properties.NullText = "描述";
            this.textEdit4.Size = new System.Drawing.Size(450, 44);
            this.textEdit4.TabIndex = 3;
            // 
            // btnSave
            // 
            this.btnSave.Location = new System.Drawing.Point(510, 553);
            this.btnSave.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);
            this.btnSave.Name = "btnSave";
            this.btnSave.Size = new System.Drawing.Size(150, 48);
            this.btnSave.TabIndex = 4;
            this.btnSave.Text = "保存";
            this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
            // 
            // labelControl1
            // 
            this.labelControl1.Appearance.Font = new System.Drawing.Font("Tahoma", 22F);
            this.labelControl1.Appearance.Options.UseFont = true;
            this.labelControl1.Location = new System.Drawing.Point(186, 30);
            this.labelControl1.Name = "labelControl1";
            this.labelControl1.Size = new System.Drawing.Size(354, 71);
            this.labelControl1.TabIndex = 5;
            this.labelControl1.Text = "修改产线信息";
            // 
            // labelControl2
            // 
            this.labelControl2.Location = new System.Drawing.Point(61, 220);
            this.labelControl2.Name = "labelControl2";
            this.labelControl2.Size = new System.Drawing.Size(120, 29);
            this.labelControl2.TabIndex = 6;
            this.labelControl2.Text = "产线编码：";
            // 
            // labelControl3
            // 
            this.labelControl3.Location = new System.Drawing.Point(61, 309);
            this.labelControl3.Name = "labelControl3";
            this.labelControl3.Size = new System.Drawing.Size(120, 29);
            this.labelControl3.TabIndex = 6;
            this.labelControl3.Text = "产线名称：";
            // 
            // labelControl4
            // 
            this.labelControl4.Location = new System.Drawing.Point(61, 381);
            this.labelControl4.Name = "labelControl4";
            this.labelControl4.Size = new System.Drawing.Size(120, 29);
            this.labelControl4.TabIndex = 6;
            this.labelControl4.Text = "车间名称：";
            // 
            // labelControl5
            // 
            this.labelControl5.Location = new System.Drawing.Point(61, 461);
            this.labelControl5.Name = "labelControl5";
            this.labelControl5.Size = new System.Drawing.Size(112, 29);
            this.labelControl5.TabIndex = 6;
            this.labelControl5.Text = "描     述：";
            // 
            // labelControl6
            // 
            this.labelControl6.Location = new System.Drawing.Point(61, 144);
            this.labelControl6.Name = "labelControl6";
            this.labelControl6.Size = new System.Drawing.Size(110, 29);
            this.labelControl6.TabIndex = 6;
            this.labelControl6.Text = "产 线 Id：";
            // 
            // textEdit3
            // 
            this.textEdit3.EditValue = "产线ID";
            this.textEdit3.Location = new System.Drawing.Point(210, 132);
            this.textEdit3.Name = "textEdit3";
            this.textEdit3.Size = new System.Drawing.Size(450, 44);
            this.textEdit3.TabIndex = 9;
            // 
            // EditProjectLineForm
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(12F, 24F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(798, 689);
            this.Controls.Add(this.textEdit3);
            this.Controls.Add(this.labelControl5);
            this.Controls.Add(this.labelControl4);
            this.Controls.Add(this.labelControl3);
            this.Controls.Add(this.labelControl6);
            this.Controls.Add(this.labelControl2);
            this.Controls.Add(this.labelControl1);
            this.Controls.Add(this.textEdit1);
            this.Controls.Add(this.textEdit2);
            this.Controls.Add(this.textEdit4);
            this.Controls.Add(this.comboBoxEdit2);
            this.Controls.Add(this.btnSave);
            this.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);
            this.Name = "EditProjectLineForm";
            this.Text = "修改产线";
            ((System.ComponentModel.ISupportInitialize)(this.textEdit1.Properties)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.textEdit2.Properties)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.comboBoxEdit2.Properties)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.textEdit4.Properties)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.textEdit3.Properties)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        }
        /// <summary>
        /// 修改产线信息的保存按钮点击事件处理方法。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                var data = new
                {
                    id = textEdit3.Text.Trim(),
                    linE_CODE = textEdit1.Text.Trim(),
                    linE_NAME = textEdit2.Text.Trim(),
                    //存Id
                    workshoP_ID = workshopNameIdMap[comboBoxEdit2.Text.Trim()],
                    linE_DESCRIPTION = textEdit4.Text.Trim()
                };
               
                using (var client = new HttpClient())
                {
                    // 将数据对象序列化为JSON字符串
                    var json = Newtonsoft.Json.JsonConvert.SerializeObject(data);
                    // 创建一个StringContent对象，指定编码和媒体类型
                    var content = new StringContent(json, Encoding.UTF8, "application/json");
                    // 使用HttpClient发送PUT请求，更新生产线信息
                    var response = await client.PutAsync("http://39.103.49.30:9982/api/ProjectLine/UpdateProductionLine", content);
                    // 读取响应内容
                    var respStr = await response.Content.ReadAsStringAsync();
                    // 解析响应JSON对象
                    var respObj = JObject.Parse(respStr);
                    // 检查响应状态码
                    if (respObj["code"]?.ToString() == "200")
                    {
                        // 如果状态码为200，显示成功消息框，触发编辑成功事件并关闭窗口
                        MessageBox.Show("修改成功！");
                        EditSuccess?.Invoke(this, EventArgs.Empty);
                        this.Close();
                 }
            else
            {
                // 如果状态码不是200，显示失败消息框，包含错误信息
                MessageBox.Show("修改失败：" + respObj["message"]);
            }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("保存出错：" + ex.Message);
            }
        }
    }
}